package com.example.consumer.consumer;

import com.example.consumer.dto.UserDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 消息消费类
 * 消费和生产引用同一个实体进行序列化和反序列化，否则反序列化失败
 * 解决方案：1.应用一个实体
 *           2.使用JsonObject存储
 * @author dzt
 * @date 2018-12-03
 * */
@Component
public class ConsumerApp {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "user-queue",durable = "true"),
            exchange = @Exchange(name = "userExchange",type = "topic"),
            key = "user.key"
        )
    )
    @RabbitHandler
    public void receiveMsg(@Payload String dto, @Headers Map<String,Object> headers, Channel channel) throws Exception{

        System.out.println(dto);

        long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
        // 手动签收
         channel.basicNack(deliveryTag,false,false);
    }

}
